'''
* @name Maiji
* @author pd87@qq.com
* @copyright m-j.cc
'''
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import codecs
import maiji
import time
import threading

# 创建程序集变量线程
JI_xiancheng = 10
JI_xiancheng_id = 0
# 当前读取的条数
JI_sqltiao = 0

def run2(limit):
	global JI_xiancheng_id
	# 读数据库获取数据
	# data = maiji.query('select * from mm_py limit %s' %limit)
	data = maiji.query('select * from mm_py where key = "" ')

	# 遍历数据库数据
	for page_i in data:
		print("正在读取数据库第 %s 条数据" % page_i[0])
		# 处理数据库读取的url(之前访问的)
		URL = page_i[2]
		a = URL[::-1]
		URL = a[a.find('/'):][::-1]
		a = a[a.find('.') + 1:a.find('/')]
		UID = a[::-1]

		# 开始访问图片内页
		data = maiji.httpget(URL + UID + '.html')
		data.encoding = 'gbk'
		html = data.text
		# print(html)
		
		# 查找页面关键词
		key = re.findall('<meta name="keywords" content="(.*?)" />',html)[0]
		
		# 查找网页描述
		desc = re.findall('<meta name="description" content="(.*?)" />',html)[0]
		desc = desc.replace('MM131','图片吧_')
		desc = desc.replace('www.mm131.com','img.ip8.cc')
		
		# 插入数据库标签和说明
		maiji.query("UPDATE `mm_py` SET `key`='%s', `desc`='%s' WHERE (`id`='%s')" % (key,desc,page_i[0]))
		
		# 获取总页数
		page = re.findall('<span class="page-ch">共(.*?)页</span>',html,re.I|re.M)[0]

		# 获取第一页，因为没有1这个id
		headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36','Referer': URL + UID + '.html'}
		data = maiji.httpget('http://img1.mm131.me/pic/' + UID + '/1.jpg','','',headers)
		maiji.ximg('G:/mm131/%s/1.jpg' % UID,data.content)

		# 开始循环内页
		i = 1
		while i < int(page):
			i = i + 1
			print("正在处理第 %s 张图片" % i)
			headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36','Referer': URL + UID + '_' + str(i) + '.html'}
			data = maiji.httpget('http://img1.mm131.me/pic/' + UID + '/%s.jpg' % i,'','',headers)
			maiji.ximg('G:/mm131/%s/%s.jpg' %(UID,i),data.content)
		time.sleep(3)
	JI_xiancheng_id = JI_xiancheng_id - 1
	runxc()

# 获取全站连接到数据库
def run1():
	# 获取网站所有导航
	nav = ['qingchun','xiaohua','chemo','qipao','mingxing','xinggan']
	for nav_i,nav_v in enumerate(nav):
		nav_i = nav_i + 1
		print("开始第 " + str(nav_i) + " 次循环 导航内容是 " + nav_v)
		
		URL = 'http://www.mm131.com/'
		# url = 'https://www.m-j.cc'
		url = URL + nav_v + '/'
		data = maiji.httpget(url)
		data.encoding = 'gb2312'

		# 替换所有特殊字符
		html = maiji.replace(data.text)
		
		# 查找当前网页的总页数
		page = re.findall('下一页</a><a href=''(.*?)'' class="page-en">末页</a>',html,re.I|re.M)[0]
		page = page[8:page.find('.')];
		print('当前导航共有 ' + str(page) + ' 页面')

		# 匹配当页图片(匹配第一页，因为他没有第一页url)
		data = re.findall('<dd><a target="_blank" href="(.*?)"><img src="(.*?)" alt="(.*?)" width="120" height="160" />(.*?)</a></dd>',html,re.I|re.M)

		for v in data:
			# 保存首页第一张图片
			a = v[1][25:]
			UID = a[:a.find('/')]
			a = a[a.find('/') + 1:-4]
			headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36','Referer': URL + '.html'}
			data = maiji.httpget(v[1],'','',headers)
			print(v[1])
			maiji.ximg('G:/mm131/%s/%s.jpg' % (UID,a),data.content)
			# print(v,nav_v)
			sql = "INSERT INTO `mm_py` (`url`, `img`, `alt`, `title`, `type`, `key`, `desc`) VALUES ('" + v[0] + "', '" + v[1] + "', '" + v[2] + "', '" + v[3] + "', '" + nav_v + "', '0', '0')"
			maiji.query(sql)
			
		# exit()
		# 开始循环内页
		i = 1
		while i < int(page):
			i = i + 1
			print('第 ' + str(i) + ' 次循环')
			url_z = url + 'list_' + str(nav_i) + '_' + str(i) + '.html'
			data = maiji.httpget(url_z)
			data.encoding = 'gb2312'
			html = maiji.replace(data.text)
			# 匹配当页图片
			data = re.findall('<dd><a target="_blank" href="(.*?)"><img src="(.*?)" alt="(.*?)" width="120" height="160" />(.*?)</a></dd>',html,re.I|re.M)
			for v in data:
				# 保存首页第一张图片
				a = v[1][25:]
				UID = a[:a.find('/')]
				a = a[a.find('/') + 1:-4]
				headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36','Referer': URL + '.html'}
				data = maiji.httpget(v[1],'','',headers)
				maiji.ximg('G:/mm131/%s/%s.jpg' % (UID,a),data.content)
				
				maiji.query("INSERT INTO `mm_py` (`url`, `img`, `alt`, `title`, `type`, `key`, `desc`) VALUES ('" + v[0] + "', '" + v[1] + "', '" + v[2] + "', '" + v[3] + "', '" + nav_v + "', '', '')")
# ====================================分割符===========================================
# run2('0,4000')
# 第一步爬所有连接，保存到数据库
# run1()
		
# 第二步（前提是数据库有记录，如果没有，请执行上面的）
# mit = input("查询格式为(0,10) 0到10条数据\r\n请输入条查询条数：\r\n")
# if mit:
	# run2(mit)

#创建多线程
# def runxc():
	# global JI_xiancheng_id
	# global JI_xiancheng
	# global JI_sqltiao
	# try:
		# tiao = 20
		# while JI_xiancheng_id < JI_xiancheng:
			# JI_xiancheng_id = JI_xiancheng_id + 1
			# can = '%s,%s' % (JI_sqltiao,tiao)
			# if JI_sqltiao < 3874:
				# JI_sqltiao = JI_sqltiao + tiao
				# print(can)
				# t = threading.Thread(target = run2,args = (can,))
				# t.start()
				# time.sleep(10)
	# except:
		# print("Error:unabletostartthread")

# runxc()



